#include "StdAfx.h"
#include "BodyRailController.h"
#include "EntityControl.h"
#include "ControlContext.h"



BodyRailController::BodyRailController(BodyRailControlLogic *logic)
{
	controlLogic = logic;
	positionCounter = 0;
	timer = 0;
	lapCounter = 0;
	stepInterval= 0.02;
	endOfRail =false;
}


BodyRailController::~BodyRailController(void)
{
}

bool BodyRailController::isSpawningNeeded()
{
	if(mapElementQueue.size()>0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

Controller::Projectile BodyRailController::getNextSpawnItem()
{
	Projectile nextProjectile = mapElementQueue.front();
	mapElementQueue.pop();

	return nextProjectile;
}


bool BodyRailController::apply(EntityControl* entity, const ControlContext& context)
{

	if(entity->isWaiting())
	{
		Ogre::Vector3 * position = controlLogic->getRail()->at(positionCounter);
		entity->setPosition(*position);
		entity->setLookAt(*(controlLogic->getRail()->at(positionCounter+15)));

		if(0<context.controlStatus.getBoundKeyValue("START_KEY"))
		{
			entity->play();
			Ogre::LogManager::getSingletonPtr()->logMessage(Ogre::LML_CRITICAL,Ogre::StringConverter::toString(entity->getPosition()));
		}
	}

	if(entity->isPlaying() && context.gameStatus==ControlContext::PLAYING )
	{

		int sizeOfMapEnd = controlLogic->getRail()->size()-165;

		if(positionCounter>=sizeOfMapEnd)
		{
			positionCounter =0;
			lapCounter++;
			stepInterval=stepInterval-(lapCounter*0.001);
			if(stepInterval<=0.01)
			{
				stepInterval=0.01;
			}
		}

		Ogre::Vector3 * position = controlLogic->getRail()->at(positionCounter);

		/*NxActor* actor = entity->getPhysicsActor()->getNxActor();
		Ogre::SceneNode* ogreNode = entity->getPhysicsActor()->getOgreNode();



		ogreNode->setPosition(position->x,position->y,position->z);
		actor->moveGlobalPosition(NxVec3(position->x,position->y,position->z));*/

		timer+= context.dt;

		if(timer>=stepInterval )
		{
			positionCounter += timer/stepInterval;

			int lookatCounter = positionCounter+15;
			if(lookatCounter>=sizeOfMapEnd)
			{
				lookatCounter = lookatCounter-sizeOfMapEnd;
			}


			entity->setPosition(*position);
			entity->setLookAt(*(controlLogic->getRail()->at(lookatCounter)));
			//positionCounter++;

			IntEntityParameter param(SCORE_PARAMETER,positionCounter+lapCounter*sizeOfMapEnd);

			entity->setParameter(SCORE_PARAMETER,&param);
			//ogreNode->lookAt(*(controlLogic->getRail()->at(positionCounter+15)),Ogre::Node::TransformSpace::TS_WORLD);
			/*else
			{
			if(positionCounter+10<(controlLogic->getRail()->size()) && !endOfRail)
			{
			endOfRail= true;
			Ogre::Vector3 railEnd = *(controlLogic->getRail()->at(positionCounter+10));
			Ogre::Vector3 dir = railEnd - *position;
			dir*= -1000;
			entity->setLookAt(*position+dir);
			}
			}*/
			timer=0;
		}
		else
		{
			timer = timer;
		}

		int maxElements = lapCounter+1;

		int spawnIndex = (positionCounter+200 < controlLogic->getRail()->size()) ? positionCounter+200 : (positionCounter+200-controlLogic->getRail()->size());

		// Generate meteors
		while ((rand() % 100 < (lapCounter+1)*5) && (maxElements-- > 0))
		{
			


			Ogre::Vector3 pos = *(controlLogic->getRail()->at(spawnIndex))+ Ogre::Vector3((rand() % 450)-225, (rand() % 450)-225, (rand() % 450)-225);
			Ogre::Vector3 scale = Ogre::Vector3(1*((float)(rand() % 150 + 50)/100.0), 1*((float)(rand() % 150 + 50)/100.0),1*((float)(rand() % 150 + 50)/100.0));

			Controller::Projectile newElement = Controller::Projectile();
			newElement.modelMeshName="Meteor1.mesh";
			newElement.collisionGroup=6;
			newElement.parcticelSystemName="explode3";
			newElement.particleSystemAdded=false;
			newElement.position=pos;
			newElement.rotate= Ogre::Vector3(0,0,0);
			newElement.scale= scale;
			newElement.velocity= Ogre::Vector3(0,0,0);

			mapElementQueue.push(newElement);
			
		}


	}

	return true;
}